package com.zofer.study;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {

    static CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

    static class TestThread extends Thread{
        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName()+"正在执行...");
                Thread.sleep(5000);
                System.out.println(Thread.currentThread().getName()+"执行完了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
        for (int i = 0; i < 3 ; i++) {
            TestThread t1 = new TestThread();
            t1.start();
        }
        System.out.println("主线程:"+Thread.currentThread().getName()+"正在等待子线程...");
        cyclicBarrier.await();
        System.out.println("主线程:"+Thread.currentThread().getName()+"等待子线程已执行完成");

    }
}
